package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyService;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;

import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;

import cn.wolfcode.wolf2w.util.ParamMap;
import com.alibaba.fastjson.JSON;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ISearchService searchService;


    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {

        //编码: %E5%B9%BF%E5%B7%9E --> 解码: 广州
        //url 传入中文时, 会自动将中文进行编码, 此时需要解码
        String kw = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(kw);


        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return searchUser(qo);
            default:
                return searchAll(qo);
        }
    }

    //查攻略
    private Object searchStrategy(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮显示)
        return JsonResult.success(new ParamMap().put("qo", qo).put("page", this.createStrategyPage(qo)));
    }

    //查用户
    private Object searchUser(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮显示)
        return JsonResult.success(new ParamMap().put("qo", qo).put("page", this.createUserInfoPage(qo)));
    }

    //查游记
    private Object searchTravel(SearchQueryObject qo) {
        //通过页面输入关键词进行全文搜索(结果需要高亮显示)
            return JsonResult.success(new ParamMap().put("qo", qo).put("page", this.createTravelPage(qo)));
    }

   /* private Object searchStrategy(SearchQueryObject qo) {
        return JsonResult.success();
    }

    //查用户
    private Object searchUser(SearchQueryObject qo) {

        return JsonResult.success();
    }

    //查游记
    private Object searchTravel(SearchQueryObject qo) {

        return JsonResult.success();
    }

    //查全部
    private Object searchAll(SearchQueryObject qo) {

        return JsonResult.success();
    }*/

    //查目的地
    private Object searchDest(SearchQueryObject qo) {
        //查询输入keyword是不是目的地
        Destination dest = destinationService.queryName(qo.getKeyword());
        //如果是, 查询该目的地下所有攻略, 游记 用户
        //result  -- total   strategys travels users
        //创建对象封装属性
        SearchResultVO vo = new  SearchResultVO();
        if(dest != null){
            //攻略
            //如果数据库中数据多,则选择一些数据显示
            List<Strategy> sts = strategyService.queryByDestName(qo.getKeyword());
            vo.setStrategys(sts);
            //游记
            List<Travel> ts = travelService.queryByDestName(qo.getKeyword());
            vo.setTravels(ts);
            //用户
            List<UserInfo> us = userInfoService.queryCity(qo.getKeyword());
            vo.setUsers(us);

            //类型转换
            //long total = sts.size() + ts.size() + us.size();
            vo.setTotal(sts.size() + ts.size() + us.size() + 0L);
        }
        //如果不是,直接返回0条
        //result total strategy travels users
        //dest
        //qo

        Map<String,Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("dest",dest);
        map.put("qo",qo);
        return JsonResult.success(map);
        //链试编程
        //return JsonResult.success(new ParamMap().put("result", vo).put("qo", qo).put("dest", dest));
    }



    //查所有
    private Object searchAll(SearchQueryObject qo) {
        SearchResultVO vo = new SearchResultVO();
        long total = 0L; //搜索结果总条数
        Page<Strategy> sts = this.createStrategyPage(qo);
        vo.setStrategys(sts.getContent());

        total += sts.getTotalElements();  //攻略分页总记录数

        Page<Travel> ts =this.createTravelPage(qo);
        vo.setTravels(ts.getContent());
        total += ts.getTotalElements();

        Page<UserInfo> us = this.createUserInfoPage(qo);
        vo.setUsers(us.getContent());
        total += us.getTotalElements();

        Page<Destination> ds = this.createDestPage(qo);
        vo.setDests(ds.getContent());
        total += ds.getTotalElements();
        vo.setTotal(total);

        return JsonResult.success(new ParamMap().put("qo", qo).put("result", vo));
    }


    private Page<Strategy> createStrategyPage(SearchQueryObject qo){
        //参数1:搜索索引 参数2:搜索类型 参数3:返回分页数据泛型 参数4:搜索条件 参数5:搜索条件列
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                Strategy.class,qo, "title", "subTitle", "summary");
        return  sts;
    }

    private Page<Travel> createTravelPage(SearchQueryObject qo){
        return   searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                Travel.class,qo, "title",  "summary");
    }
    private Page<UserInfo> createUserInfoPage(SearchQueryObject qo){
        return  searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfo.class,qo, "info", "city");
    }
    private Page<Destination> createDestPage(SearchQueryObject qo){


        Class<Destination> clz = Destination.class;
        return  searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME,
                Destination.class,qo, "name", "info");
    }



}
